﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Log.Exceptions;
using ECommerce.BLL.Order;
using ECommerce.BLL.Member;
using ECommerce.Model.Order;
using ECommerce.Model.Member;

namespace ECommerce.Web.Site
{
    public partial class Receive : System.Web.UI.Page
    {
        private ECOrderBO Order
        {
            get
            {
                return ECOrderBO.Instance<ECOrderBO>();
            }
        }

        private ECOrderPayBO OrderPay
        {
            get
            {
                return ECOrderPayBO.Instance<ECOrderPayBO>();
            }
        }

        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // MD5密钥要跟订单提交页相同，如Send.asp里的 key = "test" ,修改""号内 test 为您的密钥
            string key = "test";	// 如果您还没有设置MD5密钥请登陆我们为您提供商户后台，地址：https://merchant3.chinabank.com.cn/
            // 登陆后在上面的导航栏里可能找到“B2C”，在二级导航栏里有“MD5密钥设置”
            // 建议您设置一个16位以上的密钥或更高，密钥最多64位，但设置16位已经足够了

            string v_oid = Request["v_oid"];
            string v_pstatus = Request["v_pstatus"];
            string v_pstring = Request["v_pstring"];
            string v_pmode = Request["v_pmode"];
            string v_md5str = Request["v_md5str"];
            string v_amount = Request["v_amount"];
            string v_moneytype = Request["v_moneytype"];
            //string remark1 = Request["remark1"];

            string str = v_oid + v_pstatus + v_amount + v_moneytype + key;

            str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();

            if (str == v_md5str)
            {
                Order.dbOper = OrderPay.dbOper = Member.dbOper;

                if (v_pstatus.Equals("20"))
                {
                    //支付成功
                    //在这里商户可以写上自己的业务逻辑
                   

                    try
                    {
                        Order.dbOper.BeginTrans();

                        long orderID = long.Parse(v_oid.Split('-')[2]);


                        ECOrderPayInfo payInfo = this.OrderPay.GetInfo(orderID);
                        if (payInfo.State == (int)PayState.未支付)
                        {
                            double amount = double.Parse(v_amount);
                            double remain = this.Member.GetRemain(payInfo.MemberID);
                            MemberBaseInfo memberInfo = this.Member.GetInfo(payInfo.MemberID);
                            payInfo.MemberRemain = remain;
                            memberInfo.Remain += amount;
                            this.Member.Save(memberInfo);

                            payInfo.Bank = v_pmode;
                            payInfo.State = (int)PayState.已支付;
                            this.OrderPay.Save(payInfo);

                            if (payInfo.OrderID != null)
                            {
                                ECOrderInfo orderInfo = this.Order.GetInfo(payInfo.OrderID.Value);
                                orderInfo.MemberRemain = payInfo.MemberRemain;
                                orderInfo.PayState = (int)PayState.已支付;
                                this.Order.Save(orderInfo);

                                memberInfo.Remain -= amount;
                                //memberInfo.Integral += (int)amount;
                                this.Member.Save(memberInfo);
                            }
                           
                        }

                        Order.dbOper.CommitTrans();
                        
                    }
                    catch (Exception ex)
                    {
                        Order.dbOper.RollbackTrans();
                        ExceptionLogBO.Save(ex);
                    }

                    Response.Redirect("Shopping3.aspx?PayState=0");
                }
                else
                {

                    try
                    {
                        Order.dbOper.BeginTrans();

                        ECOrderPayInfo payInfo = this.OrderPay.GetInfo(long.Parse(v_oid));                     

                        payInfo.Bank = v_pmode;
                        payInfo.State = (int)PayState.支付失败;
                        this.OrderPay.Save(payInfo);

                        if (payInfo.OrderID != null)
                        {
                            ECOrderInfo orderInfo = this.Order.GetInfo(payInfo.OrderID.Value);                           
                            orderInfo.PayState = (int)PayState.支付失败;
                            this.Order.Save(orderInfo);
                        }

                       

                        Order.dbOper.CommitTrans();

                    }
                    catch (Exception ex)
                    {
                        Order.dbOper.RollbackTrans();
                        ExceptionLogBO.Save(ex);
                    }

                    Response.Write("支付失败");
                   // Response.Redirect("Shopping3.aspx?PayState=1");
                }
            }
            else
            {
                ExceptionLogBO.Save(new Exception("校验失败，数据可疑"));

                Response.Write("数据校验失败");
                //Response.Redirect("Shopping3.aspx?PayState=2");
                //status_msg = "校验失败，数据可疑";
            }
        }
    }
}